package fbv.gui.site;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fbv.commun.dataTools.servico.ObjetoVO;
import fbv.commun.excecoes.FBVException;
import fbv.distribuidora.sistema.delegate.SistemaDelegate;
import fbv.distribuidora.sistema.servico.Sistema;

/**
 * Servlet implementation class for Servlet: ServletControladorDeExibicao
 *
 */
 public class ServletControladorDeExibicao extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
   private Sistema sistema;
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ServletControladorDeExibicao() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		sistema = SistemaDelegate.getInstancia();
		RequestDispatcher rd = request.getRequestDispatcher("/site/produtos.jsp");
		
		try {
			Collection<ObjetoVO> collecProduto = sistema.consultarTodosProdutos();
			Collection<ObjetoVO> collecTemporaria = new ArrayList<ObjetoVO>();
			
			Object[] array = collecProduto.toArray();
			
			int inicio = 0;
			int fim = inicio + 2;
			
			for(;inicio <= fim; inicio++){
				collecTemporaria.add((ObjetoVO)array[inicio]);
			}
			request.setAttribute("produtos", collecTemporaria);
		
			rd.forward(request, response);
			
		} catch (FBVException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}   	  	    
}